import { useAxios } from "../hooks/useAxios";
import type { UseAxiosOptions } from "../hooks/useAxios";
import type { AxiosInstance, AxiosRequestConfig } from "axios";

// 用户相关api
class BaseUserApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindUserListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/list", data, config);
  }
  // 获取详情
  find = (data: FindUserBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/find", data, config);
  }
  // 保存
  save = (data: SaveUserBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/save", data, config);
  }
  // 删除
  delete = (data: DeleteUserBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/delete", data, config);
  }
  // 更新
  update = (data: UpdateUserBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/update", data, config);
  }
  // 更新状态
  updateStatus = (data: UpdateUserStatusBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/user/updateStatus", data, config);
  }
}

// Auth相关api
class BaseAuthApi {
  constructor(private readonly request: AxiosInstance) {}

  // 登录
  login = (data: AuthLoginReq, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/auth/login", data, config);
  }
  // 注册
  register = (data: AuthRegisterReq, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/auth/register", data, config);
  }
  // 重置密码
  reset = (data: AuthResetReq, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/auth/reset", data, config);
  }
  // 获取用户信息
  getUserInfo = (): Promise<ResponseData> => {
    return this.request.post("/auth/getUserInfo");
  }
  // 刷新token
  refreshToken = (): Promise<ResponseData> => {
    return this.request.post("/auth/refreshToken");;
  }
}

//Parameter相关api
class BaseParameterApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindParameterListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/parameter/list", data, config);
  }
  // 获取详情
  find = (data: FindParameterBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/parameter/find", data, config);
  }
  // 保存
  save = (data: SaveParameterBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/parameter/save", data, config);
  }
  // 删除
  delete = (data: DeleteParameterBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/parameter/delete", data, config);
  }
  // 更新
  update = (data: UpdateParameterBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/parameter/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateParameterStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/parameter/updateStatus", data, config);
  }
}

// Role相关api
class BaseRoleApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindRoleListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/list", data, config);
  }
  // 获取详情
  find = (data: FindRoleBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/find", data, config);
  }
  // 保存
  save = (data: SaveRoleBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/save", data, config);
  }
  // 删除
  delete = (data: DeleteRoleBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/delete", data, config);
  }
  // 更新
  update = (data: UpdateRoleBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/update", data, config);
  }
  // 更新状态
  updateStatus = (data: UpdateRoleStatusBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/role/updateStatus", data, config);
  }
}

// Developer相关api
class BaseDeveloperApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindDeveloperListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/developer/list", data, config);
  }
  // 获取详情
  find = (data: FindDeveloperBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/developer/find", data, config);
  }
  // 保存
  save = (data: SaveDeveloperBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/developer/save", data, config);
  }
  // 删除
  delete = (data: DeleteDeveloperBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/developer/delete", data, config);
  }
  // 更新
  update = (data: UpdateDeveloperBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/developer/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateDeveloperStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/developer/updateStatus", data, config);
  }
}

// DevApp相关api
class BaseDevAppApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindDevAppListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/devApp/list", data, config);
  }
  // 获取详情
  find = (data: FindDevAppBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/devApp/find", data, config);
  }
  // 保存
  save = (data: SaveDevAppBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/devApp/save", data, config);
  }
  // 删除
  delete = (data: DeleteDevAppBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/devApp/delete", data, config);
  }
  // 更新
  update = (data: UpdateDevAppBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/devApp/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateDevAppStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/devApp/updateStatus", data, config);
  }
}

// DictType相关api
class BaseDictTypeApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindDictTypeListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictType/list", data, config);
  }
  // 根据更新时间查询列表
  listByUpdate = (
    data: FindDictTypeListByUpdateBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/dictType/listByUpdate", data, config);
  }
  // 获取详情
  find = (data: FindDictTypeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictType/find", data, config);
  }
  // 保存
  save = (data: SaveDictTypeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictType/save", data, config);
  }
  // 删除
  delete = (data: DeleteDictTypeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictType/delete", data, config);
  }
  // 更新
  update = (data: UpdateDictTypeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictType/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateDictTypeStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/dictType/updateStatus", data, config);
  }
}

// DictData相关api
class BaseDictDataApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindDictDataListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/list", data, config);
  }
  // 获取列表树
  tree = (data: FindDictDataTreeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/tree", data, config);
  }
  // 获取详情
  find = (data: FindDictDataBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/find", data, config);
  }
  // 保存
  save = (data: SaveDictDataBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/save", data, config);
  }
  // 删除
  delete = (data: DeleteDictDataBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/delete", data, config);
  }
  // 更新
  update = (data: UpdateDictDataBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/dictData/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateDictDataStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/dictData/updateStatus", data, config);
  }
}

// SysConfig相关api
class BaseSysConfigApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取免权配置
  getConfigJson = (): Promise<ResponseData> => {
    return this.request.post("/sysConfig/getConfigJson");
  }
}

// Permission相关api
class BasePermissionApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindPermissionListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/permission/list", data, config);
  }
  // 获取详情
  find = (data: FindPermissionBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/permission/find", data, config);
  }
  // 保存
  save = (data: SavePermissionBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/permission/save", data, config);
  }
  // 删除
  delete = (data: DeletePermissionBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/permission/delete", data, config);
  }
  // 更新
  update = (data: UpdatePermissionBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/permission/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdatePermissionStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/permission/updateStatus", data, config);
  }
}

// Menu相关api
class BaseMenuApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindMenuListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/list", data, config);
  }
  // 获取树
  tree = (data: FindMenuTreeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/tree", data, config);
  }
  // 获取详情
  find = (data: FindMenuBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/find", data, config);
  }
  // 保存
  save = (data: SaveMenuBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/save", data, config);
  }
  // 删除
  delete = (data: DeleteMenuBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/delete", data, config);
  }
  // 更新
  update = (data: UpdateMenuBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menu/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateMenuStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/menu/updateStatus", data, config);
  }
  // 获取下一个排序号
  getNextSort = (
    data: GetNextSortBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/menu/getNextSort", data, config);
  }
}

// MenuGroup相关api
class BaseMenuGroupApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindMenuGroupListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/list", data, config);
  }
  // 获取树
  menuTree = (data: FindMenuGroupMenuTreeBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/menuTree", data, config);
  }
  // 获取详情
  find = (data: FindMenuGroupBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/find", data, config);
  }
  // 保存
  save = (data: SaveMenuGroupBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/save", data, config);
  }
  // 删除
  delete = (data: DeleteMenuGroupBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/delete", data, config);
  }
  // 更新
  update = (data: UpdateMenuGroupBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/menuGroup/update", data, config);
  }
  // 更新状态
  updateStatus = (
    data: UpdateMenuGroupStatusBody,
    config?: CusRequestConfig<AxiosRequestConfig>
  ): Promise<ResponseData> => {
    return this.request.post("/menuGroup/updateStatus", data, config);
  }
}

// Icon相关api
class BaseIconApi {
  constructor(private readonly request: AxiosInstance) {}

  // 获取列表
  list = (data: FindIconListBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/icon/list", data, config);
  }

  // 导入iconfont图标
  importIconfont = (data: ImportIconfontBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    const formData = new FormData();
    for (const key in data) {
      formData.append(key, data[key]);
    }
    return this.request.post("/icon/importIconfont", formData, config);
  }
  // 更新antd图标
  updateAntdIcon = (data: UpdateAntdIconBody, config?: CusRequestConfig<AxiosRequestConfig>): Promise<ResponseData> => {
    return this.request.post("/icon/updateAntdIcon", data, config);
  }

}

// 模块创建器
const moduleCreators = {
  user: (request: AxiosInstance) => new BaseUserApi(request),
  auth: (request: AxiosInstance) => new BaseAuthApi(request),
  parameter: (request: AxiosInstance) => new BaseParameterApi(request),
  role: (request: AxiosInstance) => new BaseRoleApi(request),
  developer: (request: AxiosInstance) => new BaseDeveloperApi(request),
  devApp: (request: AxiosInstance) => new BaseDevAppApi(request),
  dictType: (request: AxiosInstance) => new BaseDictTypeApi(request),
  dictData: (request: AxiosInstance) => new BaseDictDataApi(request),
  sysConfig: (request: AxiosInstance) => new BaseSysConfigApi(request),
  permission: (request: AxiosInstance) => new BasePermissionApi(request),
  menu: (request: AxiosInstance) => new BaseMenuApi(request),
  menuGroup: (request: AxiosInstance) => new BaseMenuGroupApi(request),
  icon: (request: AxiosInstance) => new BaseIconApi(request),
};

// 定义BaseApi模块
interface BaseApiModules {
  user: BaseUserApi;
  auth: BaseAuthApi;
  parameter: BaseParameterApi;
  role: BaseRoleApi;
  developer: BaseDeveloperApi;
  devApp: BaseDevAppApi;
  dictType: BaseDictTypeApi;
  dictData: BaseDictDataApi;
  sysConfig: BaseSysConfigApi;
  permission: BasePermissionApi;
  menu: BaseMenuApi;
  menuGroup: BaseMenuGroupApi;
  icon: BaseIconApi;
}

// BaseApi工厂类
class BaseApiFactory {
  private readonly request: AxiosInstance;
  private modules = new Map<keyof BaseApiModules, any>();

  constructor(options: UseAxiosOptions) {
    this.request = useAxios(options);
  }

  // 获取模块实例
  get<T extends keyof typeof moduleCreators>(moduleName: T): BaseApiModules[T] {
    if (!this.modules.has(moduleName)) {
      const creator = moduleCreators[moduleName];
      this.modules.set(moduleName, creator(this.request));
    }
    return this.modules.get(moduleName);
  }

  // 获取全量模块
  get api(): BaseApiModules {
    return new Proxy({} as BaseApiModules, {
      get: (_, moduleName: keyof BaseApiModules) => this.get(moduleName)
    });
  }
}

// 导出工厂函数
export const useBaseApiFactory = (options: UseAxiosOptions) => new BaseApiFactory(options);
